package life.system.member.service.impl;

import life.system.common.dto.MemberResponseVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import life.system.common.utils.PageUtils;
import life.system.common.utils.Query;

import life.system.member.dao.MemberDao;
import life.system.member.entity.MemberEntity;
import life.system.member.service.MemberService;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveMember(MemberResponseVo wechatUserDTO) {
        MemberEntity member = new MemberEntity();
        BeanUtils.copyProperties(wechatUserDTO, member);
        // 设置默认等级
        member.setStatus(1);
        member.setCreateTime(new Date());
    }

    @Override
    public MemberResponseVo getMemberByOpenId(String openId) {
        MemberEntity member = this.baseMapper.selectOne(new QueryWrapper<MemberEntity>().eq("open_id", openId));
        if (member == null) {
            return null;
        } else {
            MemberResponseVo wechatUserDTO = new MemberResponseVo();
            BeanUtils.copyProperties(member, wechatUserDTO);
            return wechatUserDTO;
        }
    }

}